"""
6. Z 字形变换
https://leetcode.cn/problems/zigzag-conversion/description/?envType=study-plan-v2&envId=top-interview-150
"""
class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows < 2: return s
        res = ["" for _ in range(numRows)]
        i, flag = 0, -1
        for c in s:
            res[i] += c
            if i == 0 or i == numRows - 1:
                flag = -flag
            i += flag
        return "".join(res)

if __name__ == "__main__":
    print(Solution().convert("PAYPALISHIRING", 3))
    print(Solution().convert( "PAYPALISHIRING", 4))